package com.squareup.picasso;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.NetworkInfo;

import com.squareup.picasso.Picasso.LoadedFrom;

import java.io.IOException;
import java.io.InputStream;

public abstract class RequestHandler {
    public static final class Result {
        final LoadedFrom loadedFrom;
        final Bitmap bitmap;
        final InputStream inputStream;
        final int exifRotation;

        public Result(InputStream is, LoadedFrom loadedFrom) {
            this(null, Utils.checkNotNull(is, "stream == null"), loadedFrom, 0);
        }

        public Result(Bitmap bitmap, LoadedFrom loadedFrom) {
            this(Utils.checkNotNull(bitmap, "bitmap == null"), null, loadedFrom, 0);
        }

        Result(Bitmap bitmap, InputStream inputStream, LoadedFrom loadedFrom, int exifRotation) {
            if (!(bitmap != null ^ inputStream != null)) {
                throw new AssertionError();
            }
            this.bitmap = bitmap;
            this.inputStream = inputStream;
            this.loadedFrom = Utils.checkNotNull(loadedFrom, "loadedFrom == null");
            this.exifRotation = exifRotation;
        }
    }

    public RequestHandler() {

    }

    int getRetryCount() {
        return 0;
    }

    public abstract boolean canHandleRequest(Request result);

    static boolean inJustDecodeBounds(BitmapFactory.Options options) {
        return options != null && options.inJustDecodeBounds;
    }

    static void calculateInSampleSize(int reqWidth, int reqHeight, BitmapFactory.Options options, Request request) {
        RequestHandler.calculateInSampleSize(reqWidth, reqHeight, options.outWidth, options.outHeight, options, request);
    }

    static void calculateInSampleSize(int reqWidth, int reqHeight, int width, int height, BitmapFactory.Options options, Request request) {
        int sampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            if (reqHeight == 0) {
                sampleSize = (int) Math.floor((double) ((float) width / (float) reqWidth));
            } else if (reqWidth == 0) {
                sampleSize = (int) Math.floor((double) ((float) height / (float) reqHeight));
            } else {
                sampleSize = (int) Math.floor((double) ((float) height / (float) reqHeight));
                int v1 = (int) Math.floor((double) ((float) width / (float) reqWidth));
                sampleSize = request.centerInside ? Math.max(sampleSize, v1) : Math.min(sampleSize, v1);
            }
        }

        options.inSampleSize = sampleSize;
        options.inJustDecodeBounds = false;
    }

    boolean shouldRetry(NetworkInfo networkInfo) {
        return false;
    }

    public abstract Result load(Request request) throws IOException;

    boolean supportsReplay() {
        return false;
    }

    static BitmapFactory.Options createBitmapOptions(Request request) {
        boolean justBounds = request.hasSize();
        boolean hasConfig = request.config != null;
        BitmapFactory.Options options = null;
        if ((justBounds) || hasConfig) {
            options = new BitmapFactory.Options();
            options.inJustDecodeBounds = justBounds;
            if (hasConfig) {
                options.inPreferredConfig = request.config;
            }
        }
        return options;
    }
}

